<template>
  <div class="app-container">
    <!-- 采购入库检表单页面 -->
    <el-dialog v-model="addDialog" :title="data.title" width="50%">
      <el-card>
        <template #header>
          <div class="card-header flex justify-between">
            <span>{{ data.a }}信息</span>
          </div>
        </template>
        <el-table :data="tableData" border style="width: 100%;margin: 0 auto;">
          <el-table-column :label="data.a+'编号'" prop="goodsNo" align="center" width="200"/>
          <el-table-column :label="data.a+'名称'" prop="goodsName" align="center" width="100"/>
          <el-table-column :label="data.a+'数量'" prop="goodsCount" align="center" width="100"/>
          <el-table-column label="质检比例" prop="goodsCheckRatio" align="center" width="100"/>
          <el-table-column label="合格数" prop="acceptance" align="center" width="100">
            <template #default="scope">
              <el-input
                  v-model="scope.row.acceptance"
                  type="number"
              />
            </template>
          </el-table-column>
          <el-table-column label="不合格数" prop="disqualification" align="center" width="100">
            <template #default="scope">
              <el-input
                  v-model="scope.row.disqualification"
                  type="number"
              />
            </template>
          </el-table-column>
          <el-table-column label="备注" prop="description" align="center" width="100">
            <template #default="scope">
              <el-input
                  v-model="scope.row.description"
              />
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <div style="text-align:right;margin-top:20px">
        <el-button type="primary" @click="submit" :disabled="disable">保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import {addQuality, salesPurchaseList, salesWorkList, updateInStatus} from "@/api/quality/quality.js";
import useUserStore from "@/store/modules/user.js";
import {ElMessage} from "element-plus";

let addDialog = defineModel('addDialog')
let data = defineProps(["warehouseInId","sourceNo","goodsType","title","a"])
const emit = defineEmits(['loadTableData'])
const input = ref()

const { proxy } = getCurrentInstance();
const { sale_order_status } = proxy.useDict("sale_order_status");

const form = ref({
  warehouseInId: data.warehouseInId,
  sourceNo:data.sourceNo,
  goodsType:data.goodsType,
  list:null
})

//获取登陆人id及姓名
let userId = useUserStore().id;
let userName = useUserStore().name;
// 设置查询参数中的 applyUserId 和 examineUserId
form.value.userId = userId;

let tableData = ref([])

let disable = ref(false);

// 加载数据
function getList() {
  //console.log(warehouseOutId.warehouseOutId)
  if (data.goodsType === 2){
    salesWorkList({sourceNo: data.sourceNo,goodsType:data.goodsType}).then((response) => {
      console.log(response)
      tableData.value = response.rows
      if (tableData.value != null && tableData.value[0].acceptance != null){
          for (let i = 0; i < tableData.value.length; i++) {
            tableData.value[i].goodsCount = tableData.value[i].acceptance+tableData.value[i].disqualification
          }
        disable.value = true;
      }

    })
  } else if (data.goodsType === 1){
    salesPurchaseList({sourceNo: data.sourceNo,goodsType:data.goodsType}).then((response) => {
      console.log(response)
      tableData.value = response.rows
      if (tableData.value != null && tableData.value[0].acceptance != null){
        for (let i = 0; i < tableData.value.length; i++) {
          tableData.value[i].goodsCount = tableData.value[i].acceptance+tableData.value[i].disqualification
        }
        disable.value = true;
      }
    })
  }
}

/** 保存*/
function submit(){
  form.value.list = tableData.value
  console.log(form.value)
  addQuality(form.value).then((response) => {
    ElMessage({
      message: '添加成功',
      type: 'success',
      onClose: function () {
        addDialog.value = false
        getList()
      }
    })
  })
  // 修改状态
  updateInStatus({
    warehouseInStatus:1,
    id: data.warehouseInId
  }).then(response => {
    getList()
  })
}

// 取消按钮
function cancel() {
  addDialog.value = false;
  getList();
}

getList()
</script>

<style scoped lang="scss">
</style>